package com.yc.config.rpc.interceptor;

import com.google.common.annotations.VisibleForTesting;
import io.grpc.*;
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: ZhouJianLong
 * @Date: 2024/07/01/15:49
 * @Description:
 */
@GrpcGlobalClientInterceptor
public class LogGrpcInterceptor implements ClientInterceptor {
    @VisibleForTesting
    static final Metadata.Key<String> CUSTOM_HEADER_KEY =
            Metadata.Key.of("grpc", Metadata.ASCII_STRING_MARSHALLER);

//    private static final Logger log = LoggerFactory.getLogger(LogGrpcInterceptor.class);

    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
            final MethodDescriptor<ReqT, RespT> method,
            final CallOptions callOptions,
            final Channel next) {
        return new ForwardingClientCall.SimpleForwardingClientCall<ReqT,RespT>(next.newCall(method,callOptions)){
            @Override
            public void start(Listener<RespT> responseListener, Metadata headers) {
                headers.put(CUSTOM_HEADER_KEY,"true");
                super.start(responseListener, headers);
            }
        };
    }

}
